<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Docker Compose 模板文件 | 我的笔记</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/notebook/favicon.ico">
    <script data-ad-client="ca-pub-4147143076931995" async="true" src="/notebook//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <meta name="description" content="方便阅读和寻找">
    
    <link rel="preload" href="/notebook/assets/css/0.styles.cee65b40.css" as="style"><link rel="preload" href="/notebook/assets/js/app.400f01da.js" as="script"><link rel="preload" href="/notebook/assets/js/2.ffba27f2.js" as="script"><link rel="preload" href="/notebook/assets/js/82.e355d704.js" as="script"><link rel="prefetch" href="/notebook/assets/js/10.9da57264.js"><link rel="prefetch" href="/notebook/assets/js/100.8aa79850.js"><link rel="prefetch" href="/notebook/assets/js/101.f8819976.js"><link rel="prefetch" href="/notebook/assets/js/102.7168e4aa.js"><link rel="prefetch" href="/notebook/assets/js/103.e1f049f7.js"><link rel="prefetch" href="/notebook/assets/js/104.0ada8567.js"><link rel="prefetch" href="/notebook/assets/js/105.77e95b95.js"><link rel="prefetch" href="/notebook/assets/js/106.9071202a.js"><link rel="prefetch" href="/notebook/assets/js/107.a0a63241.js"><link rel="prefetch" href="/notebook/assets/js/108.93ccf0bf.js"><link rel="prefetch" href="/notebook/assets/js/109.57caff98.js"><link rel="prefetch" href="/notebook/assets/js/11.d9d769c3.js"><link rel="prefetch" href="/notebook/assets/js/110.6aa16c3f.js"><link rel="prefetch" href="/notebook/assets/js/111.293e18e5.js"><link rel="prefetch" href="/notebook/assets/js/112.7f593e53.js"><link rel="prefetch" href="/notebook/assets/js/113.e03d7fe0.js"><link rel="prefetch" href="/notebook/assets/js/114.4fd73421.js"><link rel="prefetch" href="/notebook/assets/js/115.8dc5910d.js"><link rel="prefetch" href="/notebook/assets/js/116.fc5215b7.js"><link rel="prefetch" href="/notebook/assets/js/117.851bae37.js"><link rel="prefetch" href="/notebook/assets/js/118.03cb2186.js"><link rel="prefetch" href="/notebook/assets/js/119.17f93b7e.js"><link rel="prefetch" href="/notebook/assets/js/12.125d7a59.js"><link rel="prefetch" href="/notebook/assets/js/120.4ea767fb.js"><link rel="prefetch" href="/notebook/assets/js/121.483ad579.js"><link rel="prefetch" href="/notebook/assets/js/122.17d2596f.js"><link rel="prefetch" href="/notebook/assets/js/123.56e20e4e.js"><link rel="prefetch" href="/notebook/assets/js/124.2ec822f9.js"><link rel="prefetch" href="/notebook/assets/js/125.bccb84ed.js"><link rel="prefetch" href="/notebook/assets/js/126.e38a7466.js"><link rel="prefetch" href="/notebook/assets/js/127.43962ebd.js"><link rel="prefetch" href="/notebook/assets/js/128.23f8246b.js"><link rel="prefetch" href="/notebook/assets/js/129.e718e26c.js"><link rel="prefetch" href="/notebook/assets/js/13.f56b83ad.js"><link rel="prefetch" href="/notebook/assets/js/130.a51fe94d.js"><link rel="prefetch" href="/notebook/assets/js/131.64fb54a3.js"><link rel="prefetch" href="/notebook/assets/js/132.badc2a1a.js"><link rel="prefetch" href="/notebook/assets/js/133.72f12c19.js"><link rel="prefetch" href="/notebook/assets/js/134.0b1b2dc0.js"><link rel="prefetch" href="/notebook/assets/js/135.be2d740c.js"><link rel="prefetch" href="/notebook/assets/js/136.c3dc9ba1.js"><link rel="prefetch" href="/notebook/assets/js/137.784510e4.js"><link rel="prefetch" href="/notebook/assets/js/138.ff844e48.js"><link rel="prefetch" href="/notebook/assets/js/139.1ac43f97.js"><link rel="prefetch" href="/notebook/assets/js/14.3ed225c4.js"><link rel="prefetch" href="/notebook/assets/js/140.4c427bf3.js"><link rel="prefetch" href="/notebook/assets/js/141.459f2cb4.js"><link rel="prefetch" href="/notebook/assets/js/142.e69e2b00.js"><link rel="prefetch" href="/notebook/assets/js/143.b9cd433a.js"><link rel="prefetch" href="/notebook/assets/js/144.c12cb09a.js"><link rel="prefetch" href="/notebook/assets/js/145.43e25cc4.js"><link rel="prefetch" href="/notebook/assets/js/146.0963c075.js"><link rel="prefetch" href="/notebook/assets/js/147.37f2e690.js"><link rel="prefetch" href="/notebook/assets/js/148.631fbe69.js"><link rel="prefetch" href="/notebook/assets/js/149.a22c5490.js"><link rel="prefetch" href="/notebook/assets/js/15.10a06d24.js"><link rel="prefetch" href="/notebook/assets/js/150.e6265d54.js"><link rel="prefetch" href="/notebook/assets/js/151.aefdb9fd.js"><link rel="prefetch" href="/notebook/assets/js/152.a8c722bd.js"><link rel="prefetch" href="/notebook/assets/js/153.ec2b1513.js"><link rel="prefetch" href="/notebook/assets/js/154.4fdc765f.js"><link rel="prefetch" href="/notebook/assets/js/155.de537bd1.js"><link rel="prefetch" href="/notebook/assets/js/156.7f87247c.js"><link rel="prefetch" href="/notebook/assets/js/157.f51afdd1.js"><link rel="prefetch" href="/notebook/assets/js/158.247f3f23.js"><link rel="prefetch" href="/notebook/assets/js/159.2342ec68.js"><link rel="prefetch" href="/notebook/assets/js/16.bc052b7f.js"><link rel="prefetch" href="/notebook/assets/js/160.98e33fe9.js"><link rel="prefetch" href="/notebook/assets/js/161.b8389795.js"><link rel="prefetch" href="/notebook/assets/js/162.5809ad21.js"><link rel="prefetch" href="/notebook/assets/js/163.6d40855f.js"><link rel="prefetch" href="/notebook/assets/js/164.cd5e8c28.js"><link rel="prefetch" href="/notebook/assets/js/165.c409d97c.js"><link rel="prefetch" href="/notebook/assets/js/166.75cd78fe.js"><link rel="prefetch" href="/notebook/assets/js/167.37552374.js"><link rel="prefetch" href="/notebook/assets/js/168.d4ab1d56.js"><link rel="prefetch" href="/notebook/assets/js/169.39f3c518.js"><link rel="prefetch" href="/notebook/assets/js/17.2a8873d5.js"><link rel="prefetch" href="/notebook/assets/js/170.7993242d.js"><link rel="prefetch" href="/notebook/assets/js/171.09f40b60.js"><link rel="prefetch" href="/notebook/assets/js/172.79168981.js"><link rel="prefetch" href="/notebook/assets/js/173.a75cd141.js"><link rel="prefetch" href="/notebook/assets/js/174.bd510afd.js"><link rel="prefetch" href="/notebook/assets/js/175.c3f8ba94.js"><link rel="prefetch" href="/notebook/assets/js/176.dd9cbd79.js"><link rel="prefetch" href="/notebook/assets/js/177.f53090f5.js"><link rel="prefetch" href="/notebook/assets/js/178.bcda53b0.js"><link rel="prefetch" href="/notebook/assets/js/179.2d510aa5.js"><link rel="prefetch" href="/notebook/assets/js/18.2d0a0d6d.js"><link rel="prefetch" href="/notebook/assets/js/180.6928e4f9.js"><link rel="prefetch" href="/notebook/assets/js/181.6cc94652.js"><link rel="prefetch" href="/notebook/assets/js/182.c7d6fde2.js"><link rel="prefetch" href="/notebook/assets/js/183.1a7eaa90.js"><link rel="prefetch" href="/notebook/assets/js/184.5d52b650.js"><link rel="prefetch" href="/notebook/assets/js/185.2f8eddcd.js"><link rel="prefetch" href="/notebook/assets/js/186.2615bcf2.js"><link rel="prefetch" href="/notebook/assets/js/187.ffb3009d.js"><link rel="prefetch" href="/notebook/assets/js/188.deef127e.js"><link rel="prefetch" href="/notebook/assets/js/189.51b1958b.js"><link rel="prefetch" href="/notebook/assets/js/19.482e846f.js"><link rel="prefetch" href="/notebook/assets/js/190.a5e85724.js"><link rel="prefetch" href="/notebook/assets/js/191.fd2dfc70.js"><link rel="prefetch" href="/notebook/assets/js/192.5cb3b141.js"><link rel="prefetch" href="/notebook/assets/js/193.3a6173b0.js"><link rel="prefetch" href="/notebook/assets/js/194.2b937e4b.js"><link rel="prefetch" href="/notebook/assets/js/195.71b1b3e2.js"><link rel="prefetch" href="/notebook/assets/js/196.d8d64ba1.js"><link rel="prefetch" href="/notebook/assets/js/197.fbea3131.js"><link rel="prefetch" href="/notebook/assets/js/198.25e90057.js"><link rel="prefetch" href="/notebook/assets/js/199.5bef52d0.js"><link rel="prefetch" href="/notebook/assets/js/20.3869e9c1.js"><link rel="prefetch" href="/notebook/assets/js/200.839b8484.js"><link rel="prefetch" href="/notebook/assets/js/201.dcc87a43.js"><link rel="prefetch" href="/notebook/assets/js/202.83abe52e.js"><link rel="prefetch" href="/notebook/assets/js/203.c8c886fe.js"><link rel="prefetch" href="/notebook/assets/js/204.7d91a0aa.js"><link rel="prefetch" href="/notebook/assets/js/205.dd934d84.js"><link rel="prefetch" href="/notebook/assets/js/206.ae9d7602.js"><link rel="prefetch" href="/notebook/assets/js/207.f5ac5260.js"><link rel="prefetch" href="/notebook/assets/js/208.203ba066.js"><link rel="prefetch" href="/notebook/assets/js/209.202991be.js"><link rel="prefetch" href="/notebook/assets/js/21.95b2d828.js"><link rel="prefetch" href="/notebook/assets/js/210.3e7c6db0.js"><link rel="prefetch" href="/notebook/assets/js/211.d231f4d5.js"><link rel="prefetch" href="/notebook/assets/js/212.74210f2c.js"><link rel="prefetch" href="/notebook/assets/js/213.ca497e10.js"><link rel="prefetch" href="/notebook/assets/js/214.256f2f8d.js"><link rel="prefetch" href="/notebook/assets/js/215.e5a91195.js"><link rel="prefetch" href="/notebook/assets/js/22.bfedf3cf.js"><link rel="prefetch" href="/notebook/assets/js/23.07416482.js"><link rel="prefetch" href="/notebook/assets/js/24.54aaf5dd.js"><link rel="prefetch" href="/notebook/assets/js/25.5fddfc47.js"><link rel="prefetch" href="/notebook/assets/js/26.3beace9a.js"><link rel="prefetch" href="/notebook/assets/js/27.8166fc8b.js"><link rel="prefetch" href="/notebook/assets/js/28.45eec4fe.js"><link rel="prefetch" href="/notebook/assets/js/29.7d2c15a3.js"><link rel="prefetch" href="/notebook/assets/js/3.58b1a014.js"><link rel="prefetch" href="/notebook/assets/js/30.a7841312.js"><link rel="prefetch" href="/notebook/assets/js/31.dc9675e8.js"><link rel="prefetch" href="/notebook/assets/js/32.93c6c35d.js"><link rel="prefetch" href="/notebook/assets/js/33.9a64b2f6.js"><link rel="prefetch" href="/notebook/assets/js/34.0c220f3c.js"><link rel="prefetch" href="/notebook/assets/js/35.9f407421.js"><link rel="prefetch" href="/notebook/assets/js/36.ee8ac781.js"><link rel="prefetch" href="/notebook/assets/js/37.569079d1.js"><link rel="prefetch" href="/notebook/assets/js/38.56b32d83.js"><link rel="prefetch" href="/notebook/assets/js/39.6ea9c955.js"><link rel="prefetch" href="/notebook/assets/js/4.5042ba18.js"><link rel="prefetch" href="/notebook/assets/js/40.5bdebc85.js"><link rel="prefetch" href="/notebook/assets/js/41.8607bd5a.js"><link rel="prefetch" href="/notebook/assets/js/42.2063b1d0.js"><link rel="prefetch" href="/notebook/assets/js/43.ff59782c.js"><link rel="prefetch" href="/notebook/assets/js/44.c40eaded.js"><link rel="prefetch" href="/notebook/assets/js/45.bc61bb49.js"><link rel="prefetch" href="/notebook/assets/js/46.e9ea5687.js"><link rel="prefetch" href="/notebook/assets/js/47.a9626a0e.js"><link rel="prefetch" href="/notebook/assets/js/48.9bf986fe.js"><link rel="prefetch" href="/notebook/assets/js/49.dd90158a.js"><link rel="prefetch" href="/notebook/assets/js/5.b9679d2a.js"><link rel="prefetch" href="/notebook/assets/js/50.a9546c5a.js"><link rel="prefetch" href="/notebook/assets/js/51.dcc646ec.js"><link rel="prefetch" href="/notebook/assets/js/52.111a35ff.js"><link rel="prefetch" href="/notebook/assets/js/53.19719081.js"><link rel="prefetch" href="/notebook/assets/js/54.543f990e.js"><link rel="prefetch" href="/notebook/assets/js/55.02863756.js"><link rel="prefetch" href="/notebook/assets/js/56.262a1288.js"><link rel="prefetch" href="/notebook/assets/js/57.a80801ee.js"><link rel="prefetch" href="/notebook/assets/js/58.38d623bc.js"><link rel="prefetch" href="/notebook/assets/js/59.07a6b6b6.js"><link rel="prefetch" href="/notebook/assets/js/6.77dfe150.js"><link rel="prefetch" href="/notebook/assets/js/60.df11d23d.js"><link rel="prefetch" href="/notebook/assets/js/61.764a5b2e.js"><link rel="prefetch" href="/notebook/assets/js/62.28916805.js"><link rel="prefetch" href="/notebook/assets/js/63.f54fb2ba.js"><link rel="prefetch" href="/notebook/assets/js/64.73bb9e27.js"><link rel="prefetch" href="/notebook/assets/js/65.8e45d2c8.js"><link rel="prefetch" href="/notebook/assets/js/66.6cabc639.js"><link rel="prefetch" href="/notebook/assets/js/67.61f4e99c.js"><link rel="prefetch" href="/notebook/assets/js/68.fec9c74e.js"><link rel="prefetch" href="/notebook/assets/js/69.b530b731.js"><link rel="prefetch" href="/notebook/assets/js/7.bac04506.js"><link rel="prefetch" href="/notebook/assets/js/70.e2902def.js"><link rel="prefetch" href="/notebook/assets/js/71.09df6e0a.js"><link rel="prefetch" href="/notebook/assets/js/72.5c611ac8.js"><link rel="prefetch" href="/notebook/assets/js/73.66c43b39.js"><link rel="prefetch" href="/notebook/assets/js/74.243e6a2f.js"><link rel="prefetch" href="/notebook/assets/js/75.de524cab.js"><link rel="prefetch" href="/notebook/assets/js/76.750c2fc0.js"><link rel="prefetch" href="/notebook/assets/js/77.fa7ddd5d.js"><link rel="prefetch" href="/notebook/assets/js/78.192816fd.js"><link rel="prefetch" href="/notebook/assets/js/79.a3f3fcd9.js"><link rel="prefetch" href="/notebook/assets/js/8.48754361.js"><link rel="prefetch" href="/notebook/assets/js/80.c3fd6acb.js"><link rel="prefetch" href="/notebook/assets/js/81.7ba4627a.js"><link rel="prefetch" href="/notebook/assets/js/83.2579aea2.js"><link rel="prefetch" href="/notebook/assets/js/84.1f72cccf.js"><link rel="prefetch" href="/notebook/assets/js/85.7874726d.js"><link rel="prefetch" href="/notebook/assets/js/86.a863f0c3.js"><link rel="prefetch" href="/notebook/assets/js/87.c8c7690e.js"><link rel="prefetch" href="/notebook/assets/js/88.5d7963f9.js"><link rel="prefetch" href="/notebook/assets/js/89.9391f30b.js"><link rel="prefetch" href="/notebook/assets/js/9.c2fb6f2c.js"><link rel="prefetch" href="/notebook/assets/js/90.76bedd2e.js"><link rel="prefetch" href="/notebook/assets/js/91.e345e87a.js"><link rel="prefetch" href="/notebook/assets/js/92.175f428e.js"><link rel="prefetch" href="/notebook/assets/js/93.f45b4417.js"><link rel="prefetch" href="/notebook/assets/js/94.e3f459de.js"><link rel="prefetch" href="/notebook/assets/js/95.1632f845.js"><link rel="prefetch" href="/notebook/assets/js/96.4d0a41d5.js"><link rel="prefetch" href="/notebook/assets/js/97.39bd1831.js"><link rel="prefetch" href="/notebook/assets/js/98.54282751.js"><link rel="prefetch" href="/notebook/assets/js/99.b60593a3.js">
    <link rel="stylesheet" href="/notebook/assets/css/0.styles.cee65b40.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/notebook/" class="home-link router-link-active"><!----> <span class="site-name">我的笔记</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/notebook/guide/" class="nav-link">
  指南
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/notebook/guide/" class="nav-link">
  指南
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Docker Compose</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notebook/docker-compose/" aria-current="page" class="sidebar-link">什么是 Docker Compose</a></li><li><a href="/notebook/docker-compose/Docker Compose 安装与卸载.html" class="sidebar-link">Docker Compose 安装与卸载</a></li><li><a href="/notebook/docker-compose/Docker Compose 使用.html" class="sidebar-link">Docker Compose 使用</a></li><li><a href="/notebook/docker-compose/Docker Compose 命令说明.html" class="sidebar-link">Docker Compose 命令说明</a></li><li><a href="/notebook/docker-compose/Docker Compose 模板文件.html" class="active sidebar-link">Docker Compose 模板文件</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#build" class="sidebar-link">build</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#cap-add-cap-drop" class="sidebar-link">cap_add, cap_drop</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#command" class="sidebar-link">command</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#configs" class="sidebar-link">configs</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#cgroup-parent" class="sidebar-link">cgroup_parent</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#container-name" class="sidebar-link">container_name</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#deploy" class="sidebar-link">deploy</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#devices" class="sidebar-link">devices</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#depends-on" class="sidebar-link">depends_on</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#dns" class="sidebar-link">dns</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#dns-search" class="sidebar-link">dns_search</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#tmpfs" class="sidebar-link">tmpfs</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#env-file" class="sidebar-link">env_file</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#environment" class="sidebar-link">environment</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#expose" class="sidebar-link">expose</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#external-links" class="sidebar-link">external_links</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#extra-hosts" class="sidebar-link">extra_hosts</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#healthcheck" class="sidebar-link">healthcheck</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#image" class="sidebar-link">image</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#labels" class="sidebar-link">labels</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#links" class="sidebar-link">links</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#logging" class="sidebar-link">logging</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#network-mode" class="sidebar-link">network_mode</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#networks" class="sidebar-link">networks</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#pid" class="sidebar-link">pid</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#ports" class="sidebar-link">ports</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#secrets" class="sidebar-link">secrets</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#security-opt" class="sidebar-link">security_opt</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#stop-signal" class="sidebar-link">stop_signal</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#sysctls" class="sidebar-link">sysctls</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#ulimits" class="sidebar-link">ulimits</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#volumes" class="sidebar-link">volumes</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#其它指令" class="sidebar-link">其它指令</a></li><li class="sidebar-sub-header"><a href="/notebook/docker-compose/Docker Compose 模板文件.html#读取变量" class="sidebar-link">读取变量</a></li></ul></li><li><a href="/notebook/docker-compose/Docker Compose 实战 Tomcat.html" class="sidebar-link">Docker Compose 实战 Tomcat</a></li><li><a href="/notebook/docker-compose/Docker Compose 实战 MySQL.html" class="sidebar-link">Docker Compose 实战 MySQL</a></li><li><a href="/notebook/docker-compose/Docker Compose 部署项目到容器.html" class="sidebar-link">Docker Compose 部署项目到容器</a></li><li><a href="/notebook/docker-compose/Docker Compose 常用命令.html" class="sidebar-link">Docker Compose 常用命令</a></li><li><a href="/notebook/docker-compose/YAML 配置文件语言.html" class="sidebar-link">YAML 配置文件语言</a></li><li><a href="/notebook/docker-compose/附：为什么说 JSON 不适合做配置文件？.html" class="sidebar-link">附：为什么说 JSON 不适合做配置文件？</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="docker-compose-模板文件"><a href="#docker-compose-模板文件" class="header-anchor">#</a> Docker Compose 模板文件</h1> <p>模板文件是使用 <code>Compose</code> 的核心，涉及到的指令关键字也比较多。但大家不用担心，这里面大部分指令跟 <code>docker run</code> 相关参数的含义都是类似的。</p> <p>默认的模板文件名称为 <code>docker-compose.yml</code>，格式为 YAML 格式。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3&quot;</span>

<span class="token key atrule">services</span><span class="token punctuation">:</span>
  <span class="token key atrule">webapp</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> examples/web
    <span class="token key atrule">ports</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token string">&quot;80:80&quot;</span>
    <span class="token key atrule">volumes</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> <span class="token string">&quot;/data&quot;</span>
</code></pre></div><p>注意每个服务都必须通过 <code>image</code> 指令指定镜像或 <code>build</code> 指令（需要 Dockerfile）等来自动构建生成镜像。</p> <p>如果使用 <code>build</code> 指令，在 <code>Dockerfile</code> 中设置的选项(例如：<code>CMD</code>, <code>EXPOSE</code>, <code>VOLUME</code>, <code>ENV</code> 等) 将会自动被获取，无需在 <code>docker-compose.yml</code> 中再次设置。</p> <p>下面分别介绍各个指令的用法。</p> <h2 id="build"><a href="#build" class="header-anchor">#</a> <code>build</code></h2> <p>指定 <code>Dockerfile</code> 所在文件夹的路径（可以是绝对路径，或者相对 docker-compose.yml 文件的路径）。 <code>Compose</code> 将会利用它自动构建这个镜像，然后使用这个镜像。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3'</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>

  <span class="token key atrule">webapp</span><span class="token punctuation">:</span>
    <span class="token key atrule">build</span><span class="token punctuation">:</span> ./dir
</code></pre></div><p>你也可以使用 <code>context</code> 指令指定 <code>Dockerfile</code> 所在文件夹的路径。</p> <p>使用 <code>dockerfile</code> 指令指定 <code>Dockerfile</code> 文件名。</p> <p>使用 <code>arg</code> 指令指定构建镜像时的变量。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3'</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>

  <span class="token key atrule">webapp</span><span class="token punctuation">:</span>
    <span class="token key atrule">build</span><span class="token punctuation">:</span>
      <span class="token key atrule">context</span><span class="token punctuation">:</span> ./dir
      <span class="token key atrule">dockerfile</span><span class="token punctuation">:</span> Dockerfile<span class="token punctuation">-</span>alternate
      <span class="token key atrule">args</span><span class="token punctuation">:</span>
        <span class="token key atrule">buildno</span><span class="token punctuation">:</span> <span class="token number">1</span>
</code></pre></div><p>使用 <code>cache_from</code> 指定构建镜像的缓存</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">build</span><span class="token punctuation">:</span>
  <span class="token key atrule">context</span><span class="token punctuation">:</span> .
  <span class="token key atrule">cache_from</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> alpine<span class="token punctuation">:</span>latest
    <span class="token punctuation">-</span> corp/web_app<span class="token punctuation">:</span><span class="token number">3.14</span>
</code></pre></div><h2 id="cap-add-cap-drop"><a href="#cap-add-cap-drop" class="header-anchor">#</a> <code>cap_add, cap_drop</code></h2> <p>指定容器的内核能力（capacity）分配。</p> <p>例如，让容器拥有所有能力可以指定为：</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">cap_add</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> ALL
</code></pre></div><p>去掉 NET_ADMIN 能力可以指定为：</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">cap_drop</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> NET_ADMIN
</code></pre></div><h2 id="command"><a href="#command" class="header-anchor">#</a> <code>command</code></h2> <p>覆盖容器启动后默认执行的命令。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">command</span><span class="token punctuation">:</span> echo &quot;hello world&quot;
</code></pre></div><h2 id="configs"><a href="#configs" class="header-anchor">#</a> <code>configs</code></h2> <p>仅用于 <code>Swarm mode</code></p> <h2 id="cgroup-parent"><a href="#cgroup-parent" class="header-anchor">#</a> <code>cgroup_parent</code></h2> <p>指定父 <code>cgroup</code> 组，意味着将继承该组的资源限制。</p> <p>例如，创建了一个 cgroup 组名称为 <code>cgroups_1</code>。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">cgroup_parent</span><span class="token punctuation">:</span> cgroups_1
</code></pre></div><h2 id="container-name"><a href="#container-name" class="header-anchor">#</a> <code>container_name</code></h2> <p>指定容器名称。默认将会使用 <code>项目名称_服务名称_序号</code> 这样的格式。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">container_name</span><span class="token punctuation">:</span> docker<span class="token punctuation">-</span>web<span class="token punctuation">-</span>container
</code></pre></div><blockquote><p>注意: 指定容器名称后，该服务将无法进行扩展（scale），因为 Docker 不允许多个容器具有相同的名称。</p></blockquote> <h2 id="deploy"><a href="#deploy" class="header-anchor">#</a> <code>deploy</code></h2> <p>仅用于 <code>Swarm mode</code></p> <h2 id="devices"><a href="#devices" class="header-anchor">#</a> <code>devices</code></h2> <p>指定设备映射关系。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">devices</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token string">&quot;/dev/ttyUSB1:/dev/ttyUSB0&quot;</span>
</code></pre></div><h2 id="depends-on"><a href="#depends-on" class="header-anchor">#</a> <code>depends_on</code></h2> <p>解决容器的依赖、启动先后的问题。以下例子中会先启动 <code>redis</code> <code>db</code> 再启动 <code>web</code></p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">'3'</span>

<span class="token key atrule">services</span><span class="token punctuation">:</span>
  <span class="token key atrule">web</span><span class="token punctuation">:</span>
    <span class="token key atrule">build</span><span class="token punctuation">:</span> .
    <span class="token key atrule">depends_on</span><span class="token punctuation">:</span>
      <span class="token punctuation">-</span> db
      <span class="token punctuation">-</span> redis

  <span class="token key atrule">redis</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> redis

  <span class="token key atrule">db</span><span class="token punctuation">:</span>
    <span class="token key atrule">image</span><span class="token punctuation">:</span> postgres
</code></pre></div><blockquote><p>注意：<code>web</code> 服务不会等待 <code>redis</code> <code>db</code> 「完全启动」之后才启动。</p></blockquote> <h2 id="dns"><a href="#dns" class="header-anchor">#</a> <code>dns</code></h2> <p>自定义 <code>DNS</code> 服务器。可以是一个值，也可以是一个列表。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">dns</span><span class="token punctuation">:</span> 8.8.8.8

<span class="token key atrule">dns</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> 8.8.8.8
  <span class="token punctuation">-</span> 114.114.114.114
</code></pre></div><h2 id="dns-search"><a href="#dns-search" class="header-anchor">#</a> <code>dns_search</code></h2> <p>配置 <code>DNS</code> 搜索域。可以是一个值，也可以是一个列表。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">dns_search</span><span class="token punctuation">:</span> example.com

<span class="token key atrule">dns_search</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> domain1.example.com
  <span class="token punctuation">-</span> domain2.example.com
</code></pre></div><h2 id="tmpfs"><a href="#tmpfs" class="header-anchor">#</a> <code>tmpfs</code></h2> <p>挂载一个 tmpfs 文件系统到容器。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">tmpfs</span><span class="token punctuation">:</span> /run
<span class="token key atrule">tmpfs</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> /run
  <span class="token punctuation">-</span> /tmp
</code></pre></div><h2 id="env-file"><a href="#env-file" class="header-anchor">#</a> <code>env_file</code></h2> <p>从文件中获取环境变量，可以为单独的文件路径或列表。</p> <p>如果通过 <code>docker-compose -f FILE</code> 方式来指定 Compose 模板文件，则 <code>env_file</code> 中变量的路径会基于模板文件路径。</p> <p>如果有变量名称与 <code>environment</code> 指令冲突，则按照惯例，以后者为准。</p> <div class="language-bash extra-class"><pre class="language-bash"><code>env_file: .env

env_file:
  - ./common.env
  - ./apps/web.env
  - /opt/secrets.env
</code></pre></div><p>环境变量文件中每一行必须符合格式，支持 <code>#</code> 开头的注释行。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># common.env: Set development environment</span>
<span class="token assign-left variable">PROG_ENV</span><span class="token operator">=</span>development
</code></pre></div><h2 id="environment"><a href="#environment" class="header-anchor">#</a> <code>environment</code></h2> <p>设置环境变量。你可以使用数组或字典两种格式。</p> <p>只给定名称的变量会自动获取运行 Compose 主机上对应变量的值，可以用来防止泄露不必要的数据。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">environment</span><span class="token punctuation">:</span>
  <span class="token key atrule">RACK_ENV</span><span class="token punctuation">:</span> development
  <span class="token key atrule">SESSION_SECRET</span><span class="token punctuation">:</span>

<span class="token key atrule">environment</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> RACK_ENV=development
  <span class="token punctuation">-</span> SESSION_SECRET
</code></pre></div><p>如果变量名称或者值中用到 <code>true|false，yes|no</code> 等表达 <a href="http://yaml.org/type/bool.html" target="_blank" rel="noopener noreferrer">布尔<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 含义的词汇，最好放到引号里，避免 YAML 自动解析某些内容为对应的布尔语义。这些特定词汇，包括</p> <div class="language-bash extra-class"><pre class="language-bash"><code>y<span class="token operator">|</span>Y<span class="token operator">|</span><span class="token function">yes</span><span class="token operator">|</span>Yes<span class="token operator">|</span>YES<span class="token operator">|</span>n<span class="token operator">|</span>N<span class="token operator">|</span>no<span class="token operator">|</span>No<span class="token operator">|</span>NO<span class="token operator">|</span><span class="token boolean">true</span><span class="token operator">|</span>True<span class="token operator">|</span>TRUE<span class="token operator">|</span><span class="token boolean">false</span><span class="token operator">|</span>False<span class="token operator">|</span>FALSE<span class="token operator">|</span>on<span class="token operator">|</span>On<span class="token operator">|</span>ON<span class="token operator">|</span>off<span class="token operator">|</span>Off<span class="token operator">|</span>OFF
</code></pre></div><h2 id="expose"><a href="#expose" class="header-anchor">#</a> <code>expose</code></h2> <p>暴露端口，但不映射到宿主机，只被连接的服务访问。</p> <p>仅可以指定内部端口为参数</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">expose</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;3000&quot;</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;8000&quot;</span>
</code></pre></div><h2 id="external-links"><a href="#external-links" class="header-anchor">#</a> <code>external_links</code></h2> <blockquote><p>注意：不建议使用该指令。</p></blockquote> <p>链接到 <code>docker-compose.yml</code> 外部的容器，甚至并非 <code>Compose</code> 管理的外部容器。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">external_links</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> redis_1
 <span class="token punctuation">-</span> project_db_1<span class="token punctuation">:</span>mysql
 <span class="token punctuation">-</span> project_db_1<span class="token punctuation">:</span>postgresql
</code></pre></div><h2 id="extra-hosts"><a href="#extra-hosts" class="header-anchor">#</a> <code>extra_hosts</code></h2> <p>类似 Docker 中的 <code>--add-host</code> 参数，指定额外的 host 名称映射信息。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">extra_hosts</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;googledns:8.8.8.8&quot;</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;dockerhub:52.1.157.61&quot;</span>
</code></pre></div><p>会在启动后的服务容器中 <code>/etc/hosts</code> 文件中添加如下两条条目。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token number">8.8</span>.8.8 googledns
<span class="token number">52.1</span>.157.61 dockerhub
</code></pre></div><h2 id="healthcheck"><a href="#healthcheck" class="header-anchor">#</a> <code>healthcheck</code></h2> <p>通过命令检查容器是否健康运行。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">healthcheck</span><span class="token punctuation">:</span>
  <span class="token key atrule">test</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;CMD&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;curl&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;-f&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;http://localhost&quot;</span><span class="token punctuation">]</span>
  <span class="token key atrule">interval</span><span class="token punctuation">:</span> 1m30s
  <span class="token key atrule">timeout</span><span class="token punctuation">:</span> 10s
  <span class="token key atrule">retries</span><span class="token punctuation">:</span> <span class="token number">3</span>
</code></pre></div><h2 id="image"><a href="#image" class="header-anchor">#</a> <code>image</code></h2> <p>指定为镜像名称或镜像 ID。如果镜像在本地不存在，<code>Compose</code> 将会尝试拉取这个镜像。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">image</span><span class="token punctuation">:</span> ubuntu
<span class="token key atrule">image</span><span class="token punctuation">:</span> orchardup/postgresql
<span class="token key atrule">image</span><span class="token punctuation">:</span> a4bc65fd
</code></pre></div><h2 id="labels"><a href="#labels" class="header-anchor">#</a> <code>labels</code></h2> <p>为容器添加 Docker 元数据（metadata）信息。例如可以为容器添加辅助说明信息。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">labels</span><span class="token punctuation">:</span>
  <span class="token key atrule">com.startupteam.description</span><span class="token punctuation">:</span> <span class="token string">&quot;webapp for a startup team&quot;</span>
  <span class="token key atrule">com.startupteam.department</span><span class="token punctuation">:</span> <span class="token string">&quot;devops department&quot;</span>
  <span class="token key atrule">com.startupteam.release</span><span class="token punctuation">:</span> <span class="token string">&quot;rc3 for v1.0&quot;</span>
</code></pre></div><h2 id="links"><a href="#links" class="header-anchor">#</a> <code>links</code></h2> <blockquote><p>注意：不推荐使用该指令。</p></blockquote> <h2 id="logging"><a href="#logging" class="header-anchor">#</a> <code>logging</code></h2> <p>配置日志选项。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">logging</span><span class="token punctuation">:</span>
  <span class="token key atrule">driver</span><span class="token punctuation">:</span> syslog
  <span class="token key atrule">options</span><span class="token punctuation">:</span>
    <span class="token key atrule">syslog-address</span><span class="token punctuation">:</span> <span class="token string">&quot;tcp://192.168.0.42:123&quot;</span>
</code></pre></div><p>目前支持三种日志驱动类型。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">driver</span><span class="token punctuation">:</span> <span class="token string">&quot;json-file&quot;</span>
<span class="token key atrule">driver</span><span class="token punctuation">:</span> <span class="token string">&quot;syslog&quot;</span>
<span class="token key atrule">driver</span><span class="token punctuation">:</span> <span class="token string">&quot;none&quot;</span>
</code></pre></div><p><code>options</code> 配置日志驱动的相关参数。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">options</span><span class="token punctuation">:</span>
  <span class="token key atrule">max-size</span><span class="token punctuation">:</span> <span class="token string">&quot;200k&quot;</span>
  <span class="token key atrule">max-file</span><span class="token punctuation">:</span> <span class="token string">&quot;10&quot;</span>
</code></pre></div><h2 id="network-mode"><a href="#network-mode" class="header-anchor">#</a> <code>network_mode</code></h2> <p>设置网络模式。使用和 <code>docker run</code> 的 <code>--network</code> 参数一样的值。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">network_mode</span><span class="token punctuation">:</span> <span class="token string">&quot;bridge&quot;</span>
<span class="token key atrule">network_mode</span><span class="token punctuation">:</span> <span class="token string">&quot;host&quot;</span>
<span class="token key atrule">network_mode</span><span class="token punctuation">:</span> <span class="token string">&quot;none&quot;</span>
<span class="token key atrule">network_mode</span><span class="token punctuation">:</span> <span class="token string">&quot;service:[service name]&quot;</span>
<span class="token key atrule">network_mode</span><span class="token punctuation">:</span> <span class="token string">&quot;container:[container name/id]&quot;</span>
</code></pre></div><h2 id="networks"><a href="#networks" class="header-anchor">#</a> <code>networks</code></h2> <p>配置容器连接的网络。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3&quot;</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>

  <span class="token key atrule">some-service</span><span class="token punctuation">:</span>
    <span class="token key atrule">networks</span><span class="token punctuation">:</span>
     <span class="token punctuation">-</span> some<span class="token punctuation">-</span>network
     <span class="token punctuation">-</span> other<span class="token punctuation">-</span>network

<span class="token key atrule">networks</span><span class="token punctuation">:</span>
  <span class="token key atrule">some-network</span><span class="token punctuation">:</span>
  <span class="token key atrule">other-network</span><span class="token punctuation">:</span>
</code></pre></div><h2 id="pid"><a href="#pid" class="header-anchor">#</a> <code>pid</code></h2> <p>跟主机系统共享进程命名空间。打开该选项的容器之间，以及容器和宿主机系统之间可以通过进程 ID 来相互访问和操作。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">pid</span><span class="token punctuation">:</span> <span class="token string">&quot;host&quot;</span>
</code></pre></div><h2 id="ports"><a href="#ports" class="header-anchor">#</a> <code>ports</code></h2> <p>暴露端口信息。</p> <p>使用宿主端口：容器端口 <code>(HOST:CONTAINER)</code> 格式，或者仅仅指定容器的端口（宿主将会随机选择端口）都可以。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">ports</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;3000&quot;</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;8000:8000&quot;</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;49100:22&quot;</span>
 <span class="token punctuation">-</span> <span class="token string">&quot;127.0.0.1:8001:8001&quot;</span>
</code></pre></div><p><em>注意：当使用 <code>HOST:CONTAINER</code> 格式来映射端口时，如果你使用的容器端口小于 60 并且没放到引号里，可能会得到错误结果，因为 <code>YAML</code> 会自动解析 <code>xx:yy</code> 这种数字格式为 60 进制。为避免出现这种问题，建议数字串都采用引号包括起来的字符串格式。</em></p> <h2 id="secrets"><a href="#secrets" class="header-anchor">#</a> <code>secrets</code></h2> <p>存储敏感数据，例如 <code>mysql</code> 服务密码。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3.1&quot;</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>

<span class="token key atrule">mysql</span><span class="token punctuation">:</span>
  <span class="token key atrule">image</span><span class="token punctuation">:</span> mysql
  <span class="token key atrule">environment</span><span class="token punctuation">:</span>
    <span class="token key atrule">MYSQL_ROOT_PASSWORD_FILE</span><span class="token punctuation">:</span> /run/secrets/db_root_password
  <span class="token key atrule">secrets</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> db_root_password
    <span class="token punctuation">-</span> my_other_secret

<span class="token key atrule">secrets</span><span class="token punctuation">:</span>
  <span class="token key atrule">my_secret</span><span class="token punctuation">:</span>
    <span class="token key atrule">file</span><span class="token punctuation">:</span> ./my_secret.txt
  <span class="token key atrule">my_other_secret</span><span class="token punctuation">:</span>
    <span class="token key atrule">external</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre></div><h2 id="security-opt"><a href="#security-opt" class="header-anchor">#</a> <code>security_opt</code></h2> <p>指定容器模板标签（label）机制的默认属性（用户、角色、类型、级别等）。例如配置标签的用户名和角色名。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">security_opt</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> label<span class="token punctuation">:</span>user<span class="token punctuation">:</span>USER
    <span class="token punctuation">-</span> label<span class="token punctuation">:</span>role<span class="token punctuation">:</span>ROLE
</code></pre></div><h2 id="stop-signal"><a href="#stop-signal" class="header-anchor">#</a> <code>stop_signal</code></h2> <p>设置另一个信号来停止容器。在默认情况下使用的是 SIGTERM 停止容器。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">stop_signal</span><span class="token punctuation">:</span> SIGUSR1
</code></pre></div><h2 id="sysctls"><a href="#sysctls" class="header-anchor">#</a> <code>sysctls</code></h2> <p>配置容器内核参数。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">sysctls</span><span class="token punctuation">:</span>
  <span class="token key atrule">net.core.somaxconn</span><span class="token punctuation">:</span> <span class="token number">1024</span>
  <span class="token key atrule">net.ipv4.tcp_syncookies</span><span class="token punctuation">:</span> <span class="token number">0</span>

<span class="token key atrule">sysctls</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> net.core.somaxconn=1024
  <span class="token punctuation">-</span> net.ipv4.tcp_syncookies=0
</code></pre></div><h2 id="ulimits"><a href="#ulimits" class="header-anchor">#</a> <code>ulimits</code></h2> <p>指定容器的 ulimits 限制值。</p> <p>例如，指定最大进程数为 65535，指定文件句柄数为 20000（软限制，应用可以随时修改，不能超过硬限制） 和 40000（系统硬限制，只能 root 用户提高）。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code>  <span class="token key atrule">ulimits</span><span class="token punctuation">:</span>
    <span class="token key atrule">nproc</span><span class="token punctuation">:</span> <span class="token number">65535</span>
    <span class="token key atrule">nofile</span><span class="token punctuation">:</span>
      <span class="token key atrule">soft</span><span class="token punctuation">:</span> <span class="token number">20000</span>
      <span class="token key atrule">hard</span><span class="token punctuation">:</span> <span class="token number">40000</span>
</code></pre></div><h2 id="volumes"><a href="#volumes" class="header-anchor">#</a> <code>volumes</code></h2> <p>数据卷所挂载路径设置。可以设置宿主机路径 （<code>HOST:CONTAINER</code>） 或加上访问模式 （<code>HOST:CONTAINER:ro</code>）。</p> <p>该指令中路径支持相对路径。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">volumes</span><span class="token punctuation">:</span>
 <span class="token punctuation">-</span> /var/lib/mysql
 <span class="token punctuation">-</span> cache/<span class="token punctuation">:</span>/tmp/cache
 <span class="token punctuation">-</span> ~/configs<span class="token punctuation">:</span>/etc/configs/<span class="token punctuation">:</span>ro
</code></pre></div><h2 id="其它指令"><a href="#其它指令" class="header-anchor">#</a> 其它指令</h2> <p>此外，还有包括 <code>domainname, entrypoint, hostname, ipc, mac_address, privileged, read_only, shm_size, restart, stdin_open, tty, user, working_dir</code> 等指令，基本跟 <code>docker run</code> 中对应参数的功能一致。</p> <p>指定服务容器启动后执行的入口文件。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">entrypoint</span><span class="token punctuation">:</span> /code/entrypoint.sh
</code></pre></div><p>指定容器中运行应用的用户名。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">user</span><span class="token punctuation">:</span> nginx
</code></pre></div><p>指定容器中工作目录。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">working_dir</span><span class="token punctuation">:</span> /code
</code></pre></div><p>指定容器中搜索域名、主机名、mac 地址等。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">domainname</span><span class="token punctuation">:</span> your_website.com
<span class="token key atrule">hostname</span><span class="token punctuation">:</span> test
<span class="token key atrule">mac_address</span><span class="token punctuation">:</span> 08<span class="token punctuation">-</span>00<span class="token punctuation">-</span>27<span class="token punctuation">-</span>00<span class="token punctuation">-</span>0C<span class="token punctuation">-</span>0A
</code></pre></div><p>允许容器中运行一些特权命令。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">privileged</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre></div><p>指定容器退出后的重启策略为始终重启。该命令对保持服务始终运行十分有效，在生产环境中推荐配置为 <code>always</code> 或者 <code>unless-stopped</code>。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">restart</span><span class="token punctuation">:</span> always
</code></pre></div><p>以只读模式挂载容器的 root 文件系统，意味着不能对容器内容进行修改。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">read_only</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre></div><p>打开标准输入，可以接受外部输入。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">stdin_open</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre></div><p>模拟一个伪终端。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">tty</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre></div><h2 id="读取变量"><a href="#读取变量" class="header-anchor">#</a> 读取变量</h2> <p>Compose 模板文件支持动态读取主机的系统环境变量和当前目录下的 <code>.env</code> 文件中的变量。</p> <p>例如，下面的 Compose 文件将从运行它的环境中读取变量 <code>${MONGO_VERSION}</code> 的值，并写入执行的指令中。</p> <div class="language-yaml extra-class"><pre class="language-yaml"><code><span class="token key atrule">version</span><span class="token punctuation">:</span> <span class="token string">&quot;3&quot;</span>
<span class="token key atrule">services</span><span class="token punctuation">:</span>

<span class="token key atrule">db</span><span class="token punctuation">:</span>
  <span class="token key atrule">image</span><span class="token punctuation">:</span> <span class="token string">&quot;mongo:${MONGO_VERSION}&quot;</span>
</code></pre></div><p>如果执行 <code>MONGO_VERSION=3.2 docker-compose up</code> 则会启动一个 <code>mongo:3.2</code> 镜像的容器；如果执行 <code>MONGO_VERSION=2.8 docker-compose up</code> 则会启动一个 <code>mongo:2.8</code> 镜像的容器。</p> <p>若当前目录存在 <code>.env</code> 文件，执行 <code>docker-compose</code> 命令时将从该文件中读取变量。</p> <p>在当前目录新建 <code>.env</code> 文件并写入以下内容。</p> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token comment"># 支持 # 号注释</span>
<span class="token assign-left variable">MONGO_VERSION</span><span class="token operator">=</span><span class="token number">3.6</span>
</code></pre></div><p>执行 <code>docker-compose up</code> 则会启动一个 <code>mongo:3.6</code> 镜像的容器。</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021/4/16 上午12:21:23</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notebook/docker-compose/Docker Compose 命令说明.html" class="prev">
        Docker Compose 命令说明
      </a></span> <span class="next"><a href="/notebook/docker-compose/Docker Compose 实战 Tomcat.html">
        Docker Compose 实战 Tomcat
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/notebook/assets/js/app.400f01da.js" defer></script><script src="/notebook/assets/js/2.ffba27f2.js" defer></script><script src="/notebook/assets/js/82.e355d704.js" defer></script>
  </body>
</html>
